<?php

namespace app\api\service;

use app\api\model\ImageCategory;
use app\api\model\ImageList;
use app\api\model\NoticeList;

class CommonService
{
    public static function getNoticeNew()
    {
        $data = NoticeList::getNoticeNew();
        return [200, '操作成功', $data ?: []];
    }

    public static function getImagesList($params)
    {
        $categoryInfo = ImageCategory::getInfoById($params['category_id']);
        if (!$categoryInfo) return [201, '分类不存在', []];
        $imageList = ImageList::getListByCategoryId($params['category_id']);
        if (!$imageList) return [201, '无图片配置', []];
        $dealImage = [];
        foreach ($imageList as $item) {
            if (!$item['is_forever']) {
                list($beginTime, $endTime) = explode('~', $item['show_time']);
                if ($beginTime < time() || $endTime > time()) continue;
            }
            $dealImage[] = [
                'image_id' => $item['image_id'],
                'image_src' => dealImage($item['image_src']),
                'jump_url' => $item['jump_url'],
            ];
        }
        if (!$dealImage) return [201, '无图片配置', []];
        if ($categoryInfo['category_random']) shuffle($dealImage);
        if (!$categoryInfo['category_mode']) $dealImage = $dealImage[0];
        return [200, '操作成功', $dealImage];
    }
}